/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package yuhuibear.poat.test;

import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Types;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import yuhuibear.poat.test.entity.User;
import yuhuibear.poat.util.NameConvertor;

/**
 * 
 * @author Administrator
 */
public class Main {

	public static void main(String[] args) {
		try {
			System.out.println("__________________________________________________________________");
			testArrayNames();
			// printSqlType();
			// testReflect();
			// NameConvertor.createBean(User.class);
			// testSplit();
			// System.out.println(DatabaseTest.class.getName());
		} catch (Exception ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
		// System.out.println(DatabaseTest.class.getName());
	}

	public static void printSqlType() throws Exception {
		Field[] fds = Types.class.getDeclaredFields();
		for (Field f : fds) {
			System.out.println(f.getName() + ": " + f.get(null));
		}
	}

	public static void testSplit() {
		String st = " Log.attr as name";
		String[] ast = st.split("\\sAS\\s ");
		for (int i = 0; i < ast.length; i++) {
			System.out.println("[" + i + "] =" + ast[i]);
		}
	}

	public static void reflectFromObject() {
		Object user = (Object) new User();
		Class type = user.getClass();
		System.out.println("type = " + type);
	}

	public static void testReflect() {
		try {
			// Class type = ;
			java.beans.BeanInfo typeInfo = Introspector.getBeanInfo(User.class);

			Object bean = User.class.newInstance();
			PropertyDescriptor[] pdscp = typeInfo.getPropertyDescriptors();
			for (int i = 0; i < pdscp.length; i++) {
				PropertyDescriptor pd = pdscp[i];
				System.out.println(pd.getName() + ", display name: "
						+ pd.getDisplayName());
				Method setter = pd.getWriteMethod();
				if (setter == null) {
					continue;
				}
				if (pd.getName().equals("id")) {
					setter.invoke(bean, 2);
				}
				if (pd.getName().equals("name")) {
					setter.invoke(bean, "du-head");
				}
				if (pd.getName().equals("credite")) {
					setter.invoke(bean, 3.14);
				}
			}
			System.out.println("object:" + User.class.getName() + " : "
					+ User.class.cast(bean));
		} catch (Exception ex) {
			Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
		}
	}

	public static void testPattern() {
		Pattern p = Pattern.compile("^selEct \\w+\\(.*", Pattern.CASE_INSENSITIVE);
		String msg = "select afee_3423(09fffff, sdf33)";
		if (p.matcher(msg).matches()) {
			System.out.println(" match. ");
			// String[] ma = msg.split("\"");
			// for (String m : ma) {
			// System.out.println(m);
			// }
		}
		else {
			System.out.println(" no       match. ");
		}
	}

	public static void testArrayNames() {
		System.out.println("Long[] :" + new Long[] { 22L, 33L }.getClass());
		System.out.println("Integer[] :" + new Integer[] { 22, 33 }.getClass());
	}

	public static void testClass() {
		DatabaseTest t = new DatabaseTest();
		Class tc = t.getClass();
		System.out.println(tc.getName());
		System.out.println(NameConvertor.toTableName(tc));
	}

	public static void testSet() {
		System.out.println("test set ");
		SortedSet<Param> set;
		set = Collections.synchronizedSortedSet(new TreeSet<Param>(new ParameterMetaDataComparator()));
		set.add(new Param(5, "d"));
		set.add(new Param(1, "b"));
		set.add(new Param(3, "c"));
		set.add(new Param(9, "e"));
		set.add(new Param(0, "a"));
		Iterator<Param> itr = set.iterator();
		while (itr.hasNext()) {
			System.out.println(itr.next());
		}
		System.out.println(set.first());
		System.out.println(set.last());
	}
}

class Param implements Comparable {

	public int useCount = 0;

	public String sql;

	public Param(int n, String s) {
		useCount = n;
		sql = s;

	}

	@Override
	public String toString() {
		return useCount + ", " + sql;
	}

	@Override
	public int compareTo(Object o) {
		Param pb = (Param) o;
		if (sql.equals(pb.sql)) {
			return this.useCount - pb.useCount;
		}
		else {
			return -1;
		}
	}

	@Override
	public boolean equals(Object o) {
		Param pb = (Param) o;
		if (this.sql.equals(pb.sql)) {
			return true;
		}
		else {
			return false;

		}
	}

	@Override
	public int hashCode() {
		int hash = 5;
		hash = 19 * hash + (this.sql != null ? this.sql.hashCode() : 0);
		return hash;
	}
}

class ParameterMetaDataComparator implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		Param pb1 = (Param) o1;
		Param pb2 = (Param) o2;
		return pb1.useCount - pb2.useCount;
	}
}
